package com.jrx.anytxn.transaction.utils;

import java.util.Map;

/**
 * 模板加工工具类
 * @author lbz
 * date: 2019/12/11
 */
public class SmsTemplateUtils {

    private SmsTemplateUtils() {
    }

    /**
     * 短信模板加工
     * @param templateCon 短信模板
     * @param parameter 参数
     * @return
     */
    public static String templateProcess(String templateCon, Map<String, Object> parameter){
        String start_fix = "{";
        String end_fix = "}";

        StringBuilder buf = new StringBuilder(templateCon);
        int startIndex = buf.indexOf(start_fix);
        while (startIndex != -1) {
            int endIndex = buf.indexOf(end_fix, startIndex + start_fix.length());
            if (endIndex != -1) {
                String placeholder = buf.substring(startIndex + start_fix.length(), endIndex);
                int nextIndex;
                String propVal = parameter.get(placeholder)+"";
                buf.replace(startIndex, endIndex + end_fix.length(), propVal);
                nextIndex = startIndex + propVal.length();
                startIndex = buf.indexOf(start_fix, nextIndex);
            } else {
                startIndex = -1;
            }
        }
        return buf.toString();
    }
}
